<script>

import UserinfoEdit from '@/views/admin/userinfo/UserinfoEdit.vue'
import { PAGE_INFO } from '@/utils/MyUtils'

export default {
  name: 'UserinfoList',
  components: { UserinfoEdit },
  data () {
    return {
      pageInfo: {
        ...PAGE_INFO
      },
      searchInfo: {
        email: '',
        idcard: '',
        nickname: '',
        phone: '',
        username: '',
        status: ''
      },
      tableList: [],
      delIds: []
    }
  },
  methods: {
    getTableList () {
      this.$axios.get('userinfo/page', { params: { ...this.pageInfo, ...this.searchInfo } }).then(data => {
        this.tableList = [...data.records]
        this.pageInfo.count = data.total
      })
    },
    currentChange (value) {
      this.pageInfo.page = value
      this.getTableList()
    },
    sizeChange (value) {
      this.pageInfo.page = 1
      this.pageInfo.limit = value
      this.getTableList()
    },
    search () {
      this.pageInfo.page = 1
      this.getTableList()
    },
    clear () {
      this.pageInfo = { ...this.$options.data().pageInfo }
      this.searchInfo = { ...this.$options.data().searchInfo }
      this.getTableList()
    },
    statusChange (userinfoId) {
      this.$confirm(`确定更改编号【${userinfoId}】的状态吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.post('userinfo/status/' + userinfoId).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(e => e)
    },
    deleteData (userinfoId) {
      if (!userinfoId && this.delIds.length === 0) {
        this.$alert('请至少选中1条进行删除！', { type: 'warning' })
        return false
      }
      const deleteIds = userinfoId ? [userinfoId] : [...this.delIds]
      this.$confirm(`确定删除[${deleteIds}]的数据吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.delete('userinfo/delete', { data: deleteIds }).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(e => e)
    },
    selectionChange (selection) {
      this.delIds = [...selection.map(item => item.userinfoId)]
    },
    editData (userinfoId) {
      this.$refs.userinfoEditRef.init(userinfoId)
    }
  },
  created () {
    this.$nextTick(() => {
      this.getTableList()
    })
  }
}
</script>

<template>
<div>
  <el-card>
    <el-form :model="searchInfo" inline label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="searchInfo.username" placeholder="用户名" clearable/>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="searchInfo.nickname" placeholder="昵称" clearable/>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="searchInfo.phone" placeholder="手机号" clearable/>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="searchInfo.email" placeholder="邮箱" clearable/>
      </el-form-item>
      <el-form-item label="身份证">
        <el-input v-model="searchInfo.idcard" placeholder="身份证号码" clearable/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchInfo.status" placeholder="选择状态" clearable>
          <el-option :value="1" label="可用" />
          <el-option :value="2" label="禁用" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="search">查询</el-button>
        <el-button @click="clear">清空</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card>
    <el-button @click="editData()">添加</el-button>
    <el-button @click="deleteData()">删除</el-button>
    <el-table @selection-change="selectionChange" ref="tableRef" :data="tableList" border>
      <el-table-column type="selection" fixed="left"/>
      <el-table-column prop="userinfoId" label="编号"/>
      <el-table-column prop="username" label="用户名"/>
      <el-table-column prop="nickname" label="昵称"/>
      <el-table-column prop="phone" label="手机号"/>
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column prop="idcard" label="身份证号"/>
      <el-table-column label="状态">
        <template slot-scope="{row}">
          <el-button @click="statusChange(row.userinfoId)" title="点击切换状态" type="success" v-if="row.status === 1">可用</el-button>
          <el-button @click="statusChange(row.userinfoId)" title="点击切换状态" type="danger" v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180px" label="操作">
        <template slot-scope="{row}">
          <el-button @click="editData(row.userinfoId)">修改</el-button>
          <el-button @click="deleteData(row.userinfoId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" :total="pageInfo.count" layout="total,prev,pager,next,jumper,sizes" @current-change="currentChange" @size-change="sizeChange"/>
  </el-card>
  <!-- clear方法会删除页面的所有筛选条件并强制调回第一页，因此可以把clear理解为数据的初始化 -->
  <userinfo-edit ref="userinfoEditRef" @close="clear"/>
</div>
</template>

<style scoped lang="scss">

</style>
